package cn.gson.springboot.model.service;




import cn.gson.springboot.model.dao.ProductDao;
import cn.gson.springboot.model.mapper.ProductClassifyMapper;
import cn.gson.springboot.model.mapper.ProductMapper;
import cn.gson.springboot.model.pojos.Product;
import cn.gson.springboot.model.pojos.ProductClassify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProductService {
    @Autowired
    ProductDao productDao;
    @Autowired
    ProductMapper productMapper;

    //新增
    public void addProduct(Product product){
        productDao.save(product);//一次保存一个对象
    }

    //删除
    public void deleteOne(int productId){
        productDao.deleteById(productId);
    }

    //查找所有
    public List<Product> selectAll(String search,Integer type){
        return (List<Product>)productMapper.selectAll( search, type);
    }
    public List<ProductClassify> classify(){
        return (List<ProductClassify>)productMapper.classify();
    }

  //通过id查找
    public Product selectOne(Integer productId){
        return productMapper.selectOne(productId);
    }

    //根据id修改
    public void alter(int productId,Product product){
         if ( productDao.findById(productId)!=null){//找到该对象
             Product product1=productDao.findById(productId).get();
             product1.setProductName(product.getProductName());
             productDao.save(product1);
         }
    }

    public List<Product> selectPros(Integer productClassifyId) {
        return productMapper.selectPros(productClassifyId);
    }
}
